package com.panshi.utils;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Component
public class RedisUtils {

    public static JedisPool jedisPool;
    static Jedis jedis = null;

    static {
        //转换为流的方式
        InputStream inputStream = RedisUtils.class.getClassLoader().getResourceAsStream("redisDB.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);//加载流文件
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取配置文件失败");
        }
        String host = properties.getProperty("redis.host");
        Integer port = Integer.parseInt(properties.getProperty("redis.port"));
        Integer MaxTotal = Integer.parseInt(properties.getProperty("redis.MaxTotal"));
        Integer MaxIdle = Integer.parseInt(properties.getProperty("redis.MaxIdle"));
        Integer MinIdle = Integer.parseInt(properties.getProperty("redis.MinIdle"));

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(MaxIdle);
        jedisPoolConfig.setMinIdle(MinIdle);
        jedisPool = new JedisPool(jedisPoolConfig, host, port);
    }

    public static Jedis getJedis() {
        jedis = jedisPool.getResource();
        return jedis;
    }

    public static void release(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }

    }
}